<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="rawmaterialNum">
        <el-input
          v-model="queryParams.rawmaterialNum"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加工厂 " prop="factoryname">
        <el-input
          v-model="queryParams.factoryId"
          placeholder="请输入加工厂名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农产品id" prop="cropId">
        <el-input
          v-model="queryParams.cropname"
          placeholder="请输入农产品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原材料类别" prop="matername">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入原材料类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格(单位)" prop="unitId">
        <el-input
          v-model="queryParams.unitname"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行标准" prop="standname">
        <el-input
          v-model="queryParams.standardId"
          placeholder="请输入执行标准"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存量" prop="inventory">
        <el-input
          v-model="queryParams.inventory"
          placeholder="请输入库存量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购价格" prop="purchasePrice">
        <el-input
          v-model="queryParams.purchasePrice"
          placeholder="请输入采购价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库日期" prop="wareDate">
        <el-date-picker clearable
          v-model="queryParams.wareDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出库日期" prop="delDate">
        <el-date-picker clearable
          v-model="queryParams.delDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="相关文档" prop="document">
        <el-input
          v-model="queryParams.document"
          placeholder="请输入相关文档"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id  " align="center" prop="id" />
      <el-table-column label="编号" align="center" prop="rawmaterialNum" />
      <el-table-column label="加工厂" align="center" prop="factoryname" />
      <el-table-column label="农产品" align="center" prop="cropname" />
      <el-table-column label="类别" align="center" prop="matename" />
      <el-table-column label="规格" align="center" prop="unitname" />
      <el-table-column label="执行标准" align="center" prop="standname" />
      <el-table-column label="库存量" align="center" prop="inventory" />
      <el-table-column label="采购价格" align="center" prop="purchasePrice" />
      <el-table-column label="入库日期" align="center" prop="wareDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.wareDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库日期" align="center" prop="delDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.delDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="相关文档" align="center" prop="document" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除</el-button>

          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleAddprocess(scope.row)"
          >记录加工信息</el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="getProcessList(scope.row)"
          >查看加工信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="加工厂 " prop="factoryId">
          <el-input v-model="form.factoryId" placeholder="请输入加工厂id " />
        </el-form-item>
        <el-form-item label="农产品" prop="cropId">
          <el-input v-model="form.cropId" placeholder="请输入农产品id" />
        </el-form-item>
        <el-form-item label="类别" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入类别id" />
        </el-form-item>
        <el-form-item label="规格 " prop="unitId">
          <el-input v-model="form.unitId" placeholder="请输入规格id" />
        </el-form-item>
        <el-form-item label="执行标准" prop="standardId">
          <el-input v-model="form.standardId" placeholder="请输入执行标准id" />
        </el-form-item>
        <el-form-item label="库存量" prop="inventory">
          <el-input v-model="form.inventory" placeholder="请输入库存量" />
        </el-form-item>
        <el-form-item label="采购价格" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入采购价格" />
        </el-form-item>
        <el-form-item label="入库日期" prop="wareDate">
          <el-date-picker clearable
            v-model="form.wareDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入库日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出库日期" prop="delDate">
          <el-date-picker clearable
            v-model="form.delDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出库日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="相关文档" prop="document">
          <el-input v-model="form.document" placeholder="请输入相关文档" />
        </el-form-item>
<!--        <el-form-item label="状态" prop="status">-->
<!--          <el-input v-model="form.status" placeholder="请输入相关文档" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加加工记录信息 -->

    <el-dialog title="加工记录信息" :visible.sync="dialogFormVisible" width="1000px">
      <el-form :model="processForm">
        <el-form-item label="原材料编号" :label-width="formLabelWidth" >
          <el-input v-model="processForm.rawmaterialNum" autocomplete="off" readonly style="width: 100px;"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12"><div class="grid-content bg-purple">
            <el-form-item label="操作人员名称" :label-width="formLabelWidth" >
              <el-cascader
                v-model="opertors"
                :options="opertorGroups"></el-cascader>
            </el-form-item>
          </div></el-col>
          <el-col :span="12"><div class="grid-content bg-purple-light">
            <el-form-item label="操作人员电话" :label-width="formLabelWidth" >
              <el-input v-model="processForm.opertorPhone"  style="width: 200px;"></el-input>
            </el-form-item>
          </div></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><div class="grid-content bg-purple">
            <el-form-item label="加工阶段" :label-width="formLabelWidth" >
              <el-select v-model="processForm.scheduleId" placeholder="请选择加工阶段">
                <el-option :label="r.name" :value="r.dictValue" :key="r.dictValue" v-for="r in schedules"></el-option>
              </el-select>
            </el-form-item>
          </div></el-col>
          <el-col :span="12"><div class="grid-content bg-purple-light">
            <el-form-item label="具体操作名称" :label-width="formLabelWidth" >
              <el-input v-model="processForm.operationName"  style="width: 200px;"></el-input>
            </el-form-item>
          </div></el-col>
        </el-row>
        <el-form-item label="使用设备" :label-width="formLabelWidth" >
          <el-select v-model="processForm.equipmentId" placeholder="请选择使用设备">
            <el-option :label="r.name" :value="r.dictValue" :key="r.dictValue" v-for="r in equipments"></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="8"><div class="grid-content bg-purple">
            <el-form-item label="压力" :label-width="formLabelWidth" >
              <el-input v-model="processForm.stress"  style="width: 150px;">
                <template slot="append">Pa</template>
              </el-input>
            </el-form-item>
          </div></el-col>
          <el-col :span="8"><div class="grid-content bg-purple-light">
            <el-form-item label="温度" :label-width="formLabelWidth" >
              <el-input v-model="processForm.temperature"  style="width: 150px;">
                <template slot="append">℃</template>
              </el-input>
            </el-form-item>
          </div></el-col>
          <el-col :span="8"><div class="grid-content bg-purple">
            <el-form-item label="湿度" :label-width="formLabelWidth" >
              <el-input v-model="processForm.humidness"  style="width: 150px;">
                <template slot="append">%RH</template>
              </el-input>
            </el-form-item>
          </div></el-col>
        </el-row>
        <el-form-item label="图片" :label-width="formLabelWidth" >
          <el-upload
            class="avatar-uploader"
            action="http://localhost/dev-api/file/uploadPic"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" >
          <!--          <el-input v-model="processForm.remark"  style="width: 150px;">

                    </el-input>-->
          <vue-editor v-model="processForm.remark"></vue-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addProce">确 定</el-button>
      </div>
    </el-dialog>

    <!--  当前农作物的详细加工信息  -->
    <el-dialog title="加工信息" :visible.sync="openProcess">
      <el-table :data="processList">
        <el-table-column label="原料编号" align="center" prop="rawmaterialNum" />
        <el-table-column label="加工进度" align="center" prop="scheduleName" />
        <el-table-column label="具体操作名称" align="center" prop="operationName" />
        <el-table-column label="操作时间" align="center" prop="operationTime" width="180">
          <!--        <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                  </template>-->
        </el-table-column>
        <el-table-column label="操作员" align="center" prop="opertor" />
        <el-table-column label="操作人员联系方式" align="center" prop="opertorPhone" />
        <el-table-column label="加工设备" align="center" prop="equipmentName" />
        <el-table-column label="压力" align="center" prop="stress" />
        <el-table-column label="温度" align="center" prop="temperature" />
        <el-table-column label="湿度" align="center" prop="humidness" />
        <el-table-column label="图片" align="center" prop="pic" >
          <template slot-scope="scope">
            <img :src="scope.row.pic" style="width: 100px;height: 100px;">
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  getProcessList,
  getDicList,
  AddProcess,
  getopertor
} from "@/api/system/rawinfo";
import { VueEditor } from "vue2-editor";

export default {
  name: "Info",
  components: {
    VueEditor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rawmaterialNum: null,
        factoryId: null,
        cropId: null,
        materialId: null,
        unitId: null,
        standardId: null,
        inventory: null,
        purchasePrice: null,
        wareDate: null,
        delDate: null,
        document: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //记录跟踪加工信息
      processForm:{},
      //跟踪信息弹窗
      dialogFormVisible:false,
      formLabelWidth:"120px",
      //加工进度
      schedules:[],
      //加工设备
      equipments:[],
      //上传图片
      imageUrl:null,
      //当前农作物的所有加工记录
      processList:{},
      openProcess:false,
      opertorGroups: [],
      opertors:[]

    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        rawmaterialNum: null,
        factoryId: null,
        cropId: null,
        materialId: null,
        unitId: null,
        standardId: null,
        inventory: null,
        purchasePrice: null,
        wareDate: null,
        delDate: null,
        document: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    /** 记录加工信息 */
    handleAddprocess(row){
      this.processForm.rawmaterialId=row.id;
      this.processForm.rawmaterialNum=row.rawmaterialNum;
      this.dialogFormVisible=true;
      this.getSchedules();
      this.getEquipments();
      this.getopertorGroup();
    },
    /** 获取操作人员 */
    getopertorGroup(){
      getopertor().then(r=>{
        this.opertorGroups=r;
      })
    },
    /** 获取详细加工信息 */
    getProcessList(row){
      //根据id查process_record表的rawmaterial_id
      getProcessList(row.id).then(r=>{
        this.processList=r;
      });
      this.openProcess=true;
    },
    /** 添加跟踪记录 */
    addProce(){
      this.processForm.opertor=this.opertors[1];
      AddProcess(this.processForm).then(r=>{
        this.$modal.msgSuccess("新增成功");
        this.dialogFormVisible=false;
      })
    },
    /** 获取加工进度列表 */
    getSchedules(){
      getDicList("schedule_dic").then(r=>{
        this.schedules=r;
      })
    },
    getEquipments(){
      getDicList("equipment_dic").then(r=>{
        this.equipments=r;
      })
    },
    handleAvatarSuccess(res, file) {

        this.imageUrl="http://localhost/dev-api/file/downloadPic?fileName="+res;
      //this.imageUrl = URL.createObjectURL(file.raw);
      this.processForm.pic=this.imageUrl;

    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
